package com.bytedance.scenedemo.group.basic_usage;

import com.bytedance.scene.Scene;
import com.bytedance.scene.dialog.bottomsheet.Gravity;
import com.bytedance.scene.ui.template.AppCompatScene;
import com.bytedance.scenedemo.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.text.Layout;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;
import java.util.Optional;

/**
 * Created by JiangQi on 8/7/18.
 */
public class GroupSceneBasicUsageSample extends AppCompatScene {
    @Override
    protected Component onCreateContentView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        Component layout = inflater.parse(ResourceTable.Layout_group_scene_page_block, container, false);
        return layout;
    }

    @Override
    public void onViewCreated(Component component, Intent intent) {
        setTitle("切分 Scene 用法(add remove show hide)");
        Scene scene = BasicUsageScene.newInstance(0);
        Scene scene1 = BasicUsageScene.newInstance(1);
        Scene scene2 = BasicUsageScene.newInstance(2);
        Scene scene3 = BasicUsageScene.newInstance(3);
        if (!isAdded(scene)){
            add(ResourceTable.Id_block_0, scene, "0");
        }
        if (!isAdded(scene1)){
            add(ResourceTable.Id_block_1, scene1, "1");
        }
        if (!isAdded(scene2)){
            add(ResourceTable.Id_block_2, scene2, "2");
        }
        if (!isAdded(scene3)){
            add(ResourceTable.Id_block_3, scene3, "3");
        }
        Button button1 = (Button) component.findComponentById(ResourceTable.Id_btn_0);
        Button button2 = (Button) component.findComponentById(ResourceTable.Id_btn_1);
        Button button3 = (Button) component.findComponentById(ResourceTable.Id_btn_2);
        Button button4 = (Button) component.findComponentById(ResourceTable.Id_btn_3);
        button1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (isAdded(scene)) {
                    if (isShow(scene)) {
                        hide(scene);
                    } else {
                        show(scene);
                    }
                } else {
                    add(ResourceTable.Id_block_0, scene, "0");
                }
            }
        });
        button2.setClickedListener(component12 -> {
            if (isAdded(scene)) {
                remove(scene);
            } else {
                add(ResourceTable.Id_block_0, scene, "0");
            }
        });
        button3.setClickedListener(component1 -> { //todo 动画加载失败
            if (isAdded(scene)) {
                if (isShow(scene)) {
                    AnimatorProperty animatorProperty=new AnimatorProperty();
                    animatorProperty.moveFromX(0f).moveToX(scene.getView().getWidth()/2)
                            .alphaFrom(1f).alpha(0f)
                            .setDuration(400);
                    hide(scene,animatorProperty);
                } else {
                    AnimatorProperty animatorProperty=new AnimatorProperty();
                    animatorProperty.moveFromX(-scene.getView().getWidth()/2).moveToX(0f)
                            .alphaFrom(0f).alpha(1f)
                            .setDuration(400);
                    show(scene,animatorProperty);
                }
            } else {
                if(scene.getView()!=null){
                    AnimatorProperty animatorProperty=new AnimatorProperty();
                    animatorProperty.moveFromX(scene.getView().getWidth()).moveToX(0f)
                            .setDuration(200);
                    add(ResourceTable.Id_block_0, scene, "0",animatorProperty);
                }
            }
        });
        button4.setClickedListener(component13 -> {
            if (isAdded(scene)) {
                if (isShow(scene)) {
                    AnimatorProperty animatorProperty=new AnimatorProperty();
                    animatorProperty.moveFromX(0f).moveToX(-scene.getView().getWidth())
                            .setDuration(200);
                    hide(scene,animatorProperty);
                } else {
                    AnimatorProperty animatorProperty=new AnimatorProperty();
                    animatorProperty.moveFromX(scene.getView().getWidth()).moveToX(0f)
                            .setDuration(200);
                    show(scene,animatorProperty);
                }
            } else {
                add(ResourceTable.Id_block_0, scene, "0");
            }
        });
        super.onViewCreated(component, intent);
    }

   public static class BasicUsageScene extends Scene {
        Text textView;
        @Override
        public Component onCreateView(LayoutScatter scatter, ComponentContainer container, Intent intent) {
            StackLayout stackLayout = new StackLayout(requireAbility());
            textView = new Text(requireAbility());
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(0x00000000));
            textView.setBackground(shapeElement);
            StackLayout.LayoutConfig layoutConfig = new StackLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,ComponentContainer.LayoutConfig.MATCH_PARENT);
            layoutConfig.alignment = LayoutAlignment.CENTER;
            stackLayout.setLayoutConfig(layoutConfig);
            stackLayout.addComponent(textView,layoutConfig);
            return stackLayout;
        }

        @Override
        public void onViewCreated(Component component, Intent intent) {
            IntentParams intentParams = getArguments();
            int dex = 0;
            if(intentParams!=null){
                 dex = (int) intentParams.getParam("index");
                if(dex==0){
                    ShapeElement shapeElement = new ShapeElement();
                    shapeElement.setRgbColor(RgbColor.fromArgbInt(0xFFf36c60));
                    getView().setBackground(shapeElement);
                }else if(dex==1){
                    ShapeElement shapeElement = new ShapeElement();
                    shapeElement.setRgbColor(RgbColor.fromArgbInt(0xFFaed581));
                    getView().setBackground(shapeElement);
                }else if(dex==2){
                    ShapeElement shapeElement = new ShapeElement();
                    shapeElement.setRgbColor(RgbColor.fromArgbInt(0xFF4db6ac));
                    getView().setBackground(shapeElement);
                }else if(dex==3){
                    ShapeElement shapeElement = new ShapeElement();
                    shapeElement.setRgbColor(RgbColor.fromArgbInt(0xFFf06292));
                    getView().setBackground(shapeElement);
                }

            }
            textView.setTextAlignment(TextAlignment.CENTER);
            textView.setTextSize(15, Text.TextSizeType.FP);
            textView.setTextColor(Color.GRAY);
            textView.setText("Child Scene"+dex);
            super.onViewCreated(component, intent);
        }

       /**
         * 私有构造
         */
       private BasicUsageScene() {
       }

       public static Scene newInstance(int index){
           Scene scene = new BasicUsageScene();
           IntentParams intent = new IntentParams();
           intent.setParam("index",index);
           scene.setArguments(intent);
           return scene;
       }
    }
}
